/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RelatorioDePedidosDialog.java
 *
 * Created on Nov 12, 2010, 1:55:20 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleRelatorio;
import br.com.tbp.controle.ControleSupervisor;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.model.domain.Supervisor;
import br.com.tbp.model.domain.constant.TipoDeProduto;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Diego Freire
 */
public class RelatorioRankingEntradaDeProdutosDialog extends javax.swing.JDialog {

    private final Supervisor supervisorDefault = new Supervisor(0, "Todos os supervisores");

    public RelatorioRankingEntradaDeProdutosDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        supervisoresToCombo();
        data1TextField.setDate(new Date());
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        separador1 = new javax.swing.JSeparator();
        data1 = new javax.swing.JLabel();
        data1TextField = new org.jdesktop.swingx.JXDatePicker();
        data2 = new javax.swing.JLabel();
        data2TextField = new org.jdesktop.swingx.JXDatePicker();
        separador2 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        tipoComboBox = new javax.swing.JComboBox();
        separador3 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        supervisorComboBox = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        limparJButton = new javax.swing.JButton();
        emitirButton = new javax.swing.JButton();
        fecharButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("TBP - Relatório Ranking Entrada de Produtos");

        separador1.setForeground(new java.awt.Color(153, 153, 153));

        data1.setText("Data Inicial:");

        data2.setText("Data Final:");

        separador2.setForeground(new java.awt.Color(153, 153, 153));

        jLabel1.setText("Selecione a data inicial, a final, o tipo, o supervisor e clique para emitir o ranking.");

        jLabel2.setText("Tipo do produto:");

        tipoComboBox.setModel(new DefaultComboBoxModel(TipoDeProduto.values()));

        separador3.setForeground(new java.awt.Color(153, 153, 153));

        jLabel3.setText("Supervisor:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(data1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(data1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(data2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(data2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(separador1, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addComponent(separador2, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tipoComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(separador3, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(supervisorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 361, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(separador1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(data2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(data2)
                    .addComponent(data1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(data1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(separador2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tipoComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(separador3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(supervisorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(55, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        limparJButton.setMnemonic('l');
        limparJButton.setText("Limpar");
        limparJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparJButtonActionPerformed(evt);
            }
        });
        jPanel2.add(limparJButton);

        emitirButton.setMnemonic('e');
        emitirButton.setText("Emitir");
        emitirButton.setPreferredSize(new java.awt.Dimension(100, 26));
        emitirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emitirButtonActionPerformed(evt);
            }
        });
        jPanel2.add(emitirButton);

        fecharButton.setMnemonic('f');
        fecharButton.setText("Fechar");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });
        jPanel2.add(fecharButton);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limparJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparJButtonActionPerformed
        limparCampos();
}//GEN-LAST:event_limparJButtonActionPerformed

    private void emitirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emitirButtonActionPerformed
        Date inicio = data1TextField.getDate();
        Date fim = data2TextField.getDate();
        if (fim == null) {
            fim = inicio;
        }
        try {
            ControleRelatorio.rankingDeProdutos(inicio, fim,
                    (TipoDeProduto) tipoComboBox.getSelectedItem(),
                    (Supervisor) supervisorComboBox.getSelectedItem());
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
}//GEN-LAST:event_emitirButtonActionPerformed

    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        dispose();
}//GEN-LAST:event_fecharButtonActionPerformed
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel data1;
    private org.jdesktop.swingx.JXDatePicker data1TextField;
    private javax.swing.JLabel data2;
    private org.jdesktop.swingx.JXDatePicker data2TextField;
    private javax.swing.JButton emitirButton;
    private javax.swing.JButton fecharButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton limparJButton;
    private javax.swing.JSeparator separador1;
    private javax.swing.JSeparator separador2;
    private javax.swing.JSeparator separador3;
    private javax.swing.JComboBox supervisorComboBox;
    private javax.swing.JComboBox tipoComboBox;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    private void limparCampos() {
        data1TextField.setDate(null);
        data2TextField.setDate(null);
        data1TextField.requestFocus();
    }

    private void supervisoresToCombo() {
        try {
            ControleSupervisor controle = new ControleSupervisor();
            List<Supervisor> lista = controle.pesquisar();
            DefaultComboBoxModel model = new DefaultComboBoxModel(lista.toArray());
            model.addElement(supervisorDefault);
            supervisorComboBox.setModel(model);
            supervisorComboBox.setSelectedItem(supervisorDefault);
        } catch(PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }
}
